﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace BLL
{
  public  class PrintBall
    {
        /// <summary>
        /// 将string数组转换成int数组
        /// </summary>
        /// <param name="stringArray"></param>
        /// <returns></returns>
        public int[] intArray(string[] stringArray)
        {
            int[] intArr = new int[stringArray.Length];
            for (int i = 0; i < intArr.Length; i++)
            {
                intArr[i] = int.Parse(stringArray[i]);
            }
            return intArr;
        }

        public ArrayList combine(int[] a, int m)//a为多少个号码球，m为从中 选几个球
        {

            int n = a.Length;//一共号码球的个数
            ArrayList result = new ArrayList();
            int[] bs = new int[n];
            for (int i = 0; i < n; i++)
            {
                bs[i] = 0;
            }
            //初始化   
            for (int i = 0; i < m; i++)
            {
                bs[i] = 1;
            }
            bool flag = true;
            bool tempFlag = false;
            int pos = 0;
            int sum = 0;
            //首先找到第一个10组合，然后变成01，同时将左边所有的1移动到数组的最左边   
            do
            {
                sum = 0;
                pos = 0;
                tempFlag = true;
                result.Add(print(bs, a, m));

                for (int i = 0; i < n - 1; i++)
                {
                    if (bs[i] == 1 && bs[i + 1] == 0)
                    {
                        bs[i] = 0;
                        bs[i + 1] = 1;
                        pos = i;
                        break;
                    }
                }
                //将左边的1全部移动到数组的最左边   

                for (int i = 0; i < pos; i++)
                {
                    if (bs[i] == 1)
                    {
                        sum++;
                    }
                }
                for (int i = 0; i < pos; i++)
                {
                    if (i < sum)
                    {
                        bs[i] = 1;
                    }
                    else
                    {
                        bs[i] = 0;
                    }
                }

                //检查是否所有的1都移动到了最右边   
                for (int i = n - m; i < n; i++)
                {
                    if (bs[i] == 0)
                    {
                        tempFlag = false;
                        break;
                    }
                }
                if (tempFlag == false)
                {
                    flag = true;
                }
                else
                {
                    flag = false;
                }

            } while (flag);
            result.Add(print(bs, a, m));

            return result;
        }

        private int[] print(int[] bs, int[] a, int m)
        {
            int[] result = new int[m];
            int pos = 0;
            for (int i = 0; i < bs.Length; i++)
            {
                if (bs[i] == 1)
                {
                    result[pos] = a[i];
                    pos++;
                }
            }
            return result;
        }

        public ArrayList print(ArrayList strRed, int[] strBlue)
        {
            ArrayList arrlst = new ArrayList();
            string redBall = "";
            string aa = "";
            string bb = "";
            for (int k = 0; k < strBlue.Length; k++)//加蓝球16个号
            {
                for (int i = 0; i < strRed.Count; i++)
                {
                    if (strRed.Count == 2 && i == 1) break;
                    int[] a = (int[])strRed[i];
                    redBall = "";
                    for (int j = 0; j < a.Length; j++)
                    {
                        aa = a[j].ToString();
                        if (a[j] <= 9)
                        {
                            aa = "0" + a[j];
                        }
                        if (j >= 5)
                        {
                            //HttpContext.Current.Response.Write(aa);//120
                            redBall += aa;
                        }
                        else
                        {
                            //HttpContext.Current.Response.Write(aa + ",");//120
                            redBall += aa + ",";
                        }
                        //arrlst.Add("|");

                    }
                    //HttpContext.Current.Response.Write("|");//120
                    if (Convert.ToInt32(strBlue[k]) <= 9)
                    {
                        bb = "0" + Convert.ToInt32(strBlue[k]);

                    }
                    else
                    {
                        bb = Convert.ToInt32(strBlue[k]).ToString();
                    }
                    arrlst.Add(redBall + "," + bb);
                    //---------------------------------------------------------
                    //HttpContext.Current.Response.Write(redBall + "," + bb);
                }
            }
            //if (strRed.Count == 2)//120
            //    HttpContext.Current.Response.Write(strRed.Count - 1);//120
            //else
            //    HttpContext.Current.Response.Write(strRed.Count);//120
            return arrlst;

        }

        public void PrintRandRomBall(int[] redBall, int[] blueBall)
        {
            //int[] num = new int[] { 6 };
            //num = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
            //           27, 28, 29, 30, 31, 32, 33 };
            print(combine(redBall, 6), blueBall);//33选6

        }

    }
}
